home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / ScrapColor 1.0 / Demo source / main.c < prev    next >
C/C++ Source or Header  |  1996-06-05  |  5KB  |  266 lines

  1. // main.c
  2.  
  3. #include "main.h"
  4.  
  5. static    void DoTest(void);
  6. static    void DoInit(void);
  7. static    void DoLoop(void);
  8. static    void DoExit(void);
  9. static    void DoMenuItem(long theMenuAndItem);
  10. static    void DisplayScrapColorInfo(void);
  11. static    void DrawColorString(short model, unsigned short value, short index);
  12. static    void PutColorPickerIntoScrap(unsigned short model);
  13.  
  14. static Boolean    gDone = FALSE;
  15. static short    gScrapCount = -1;
  16.  
  17. void DoTest()
  18. {
  19.  
  20. }
  21.  
  22. void DoInit(void)
  23. {    
  24.     MaxApplZone();
  25.     MoreMasters();
  26.  
  27.     // Basic Initialization 
  28.     InitGraf(&qd.thePort);
  29.     
  30.     InitFonts();
  31.     InitWindows();
  32.     InitMenus();
  33.     TEInit();
  34.     InitDialogs(0);
  35.     InitCursor();
  36.     
  37.     InsertMenu(GetMenu(kAppleMenuID), 0);
  38.     AddResMenu(GetMHandle(kAppleMenuID), 'DRVR');
  39.     InsertMenu(GetMenu(kFileMenuID), 0);
  40.     InsertMenu(GetMenu(kEditMenuID), 0);
  41.     DrawMenuBar();
  42.     
  43.     {
  44.     Rect    aRect;
  45.         SetRect(&aRect,50,50,400,150);
  46.         NewWindow(NULL,&aRect,"\pScrap Color",TRUE,0,(WindowPtr)-1,FALSE,0L);
  47.     }
  48.  
  49. }
  50.  
  51. void PutColorPickerIntoScrap(unsigned short model)
  52. {
  53. RGBColor    rgbColor;
  54. ColorData    theColor;
  55. Point        pt;
  56.  
  57.     pt.h = pt.v = 0;
  58.     rgbColor.red = rgbColor.green = rgbColor.blue = 0;
  59.     GetColor(pt,"\pPick a color:",&rgbColor,&rgbColor);
  60.     
  61.     ConvertColor((ColorData*)&rgbColor,kRGBModel,&theColor,model);
  62.     ZeroScrap();
  63.     PutColorIntoScrap(&theColor,model);
  64. }
  65.  
  66. void DrawColorString(short model, unsigned short value, short index)
  67. {
  68. double            dpercent;
  69. unsigned short    percent;
  70. Str255            numString;
  71.  
  72.     if ((model == kHSVModel || model == kHSLModel) && index == 1 && value == 0)
  73.     {
  74.         DrawString("\pundef");
  75.         return;
  76.     }
  77.  
  78.     dpercent = ((double)value / (double)65535);
  79.     if ((model == kHSVModel || model == kHSLModel) && index == 1)
  80.     {
  81.         percent = 360 * dpercent;
  82.         NumToString(percent,numString);
  83.         numString[0]++;
  84.         numString[numString[0]] = '°';
  85.     }
  86.     else
  87.     {
  88.         percent = 100 * dpercent;
  89.         NumToString(percent,numString);
  90.         numString[0]++;
  91.         numString[numString[0]] = '%';
  92.     }
  93.     
  94.     DrawString(numString);
  95. }
  96.  
  97. void DisplayScrapColorInfo(void)
  98. {
  99. WindowPtr    theWindow;
  100. ColorData    data;
  101. short        model;
  102. Str255        theStr;
  103. short        x, strIndex;
  104. Rect        aRect;
  105.  
  106.  
  107.     theWindow = FrontWindow();
  108.     SetPort(theWindow);
  109.     TextFont(monaco);
  110.     TextSize(12);
  111.     TextFace(0);
  112.     aRect = theWindow->portRect;
  113.     EraseRect(&aRect);
  114.     if (!GetColorFromScrap(&data,&model))
  115.     {
  116.         strIndex = 128 + model;
  117.  
  118.         for (x = 1; x < 4; x++)
  119.         {
  120.             MoveTo(5,12*x);
  121.             GetIndString(theStr,strIndex,x);
  122.             DrawString(theStr);
  123.             Move(5,0);
  124.             // treat data as an array of unsigned shorts
  125.             DrawColorString(model,((unsigned short*)&data)[x-1],x);
  126.         }
  127.     }
  128. }
  129.  
  130. void DoLoop(void) 
  131. {
  132. EventRecord theEvent;
  133.  
  134.     while(!gDone) 
  135.     {
  136.     PScrapStuff    scrapInfo;
  137.         
  138.         scrapInfo = InfoScrap();
  139.         if (scrapInfo->scrapCount != gScrapCount)
  140.         {
  141.             gScrapCount = scrapInfo->scrapCount;
  142.             DisplayScrapColorInfo();
  143.         }
  144.         
  145.         WaitNextEvent(everyEvent, &theEvent, 30, 0);    // Sleep for 2.5 secs
  146.         
  147.         switch(theEvent.what) 
  148.         {
  149.             case nullEvent:
  150.                 break;
  151.                 
  152.             case mouseDown:
  153.             {
  154.             short thePart;
  155.             WindowPtr whichWin;
  156.                 
  157.                 thePart = FindWindow(theEvent.where, &whichWin);
  158.                 
  159.                 switch(thePart)
  160.                 {
  161.                     case inMenuBar:
  162.                         DoMenuItem(MenuSelect(theEvent.where));
  163.                         break;
  164.                         
  165.                     case inSysWindow:
  166.                         SystemClick(&theEvent, whichWin);
  167.                         break;
  168.                         
  169.                     case inDrag:
  170.                     {
  171.                     Rect    bounds;
  172.                     
  173.                         bounds = (*GetGrayRgn())->rgnBBox;
  174.                         DragWindow(whichWin, theEvent.where, &bounds);
  175.                     }
  176.                     break;
  177.                 }
  178.             }
  179.             break;
  180.             
  181.             case keyDown:
  182.             case autoKey:
  183.                 if (theEvent.modifiers & cmdKey)
  184.                     DoMenuItem(MenuKey(theEvent.message & charCodeMask));
  185.                 break;
  186.             case updateEvt:
  187.                 break;
  188.             case activateEvt:
  189.                 break;
  190.             case kHighLevelEvent:
  191.                 break;
  192.         }
  193.     }    
  194. }
  195.  
  196. void DoMenuItem(long theMenuAndItem)
  197. {
  198. short theMenu, theItem;
  199. Str63 theString;
  200.     
  201.     if (! theMenuAndItem) return;
  202.     
  203.     theMenu = (theMenuAndItem & 0xFFFF0000) >> 16;
  204.     theItem = theMenuAndItem & 0x0000FFFF;
  205.     
  206.     switch(theMenu) {
  207.         case kAppleMenuID:
  208.             switch (theItem)
  209.             {                                
  210.                 default:
  211.                 {
  212.                     GetItem(GetMHandle(theMenu), theItem, theString);
  213.                     OpenDeskAcc(theString);
  214.                 }
  215.                 break;
  216.             }
  217.             break;
  218.             
  219.         case kFileMenuID:
  220.             switch(theItem)
  221.             {
  222.                 case kFileMenuQuitItem:
  223.                     gDone = TRUE;
  224.                     break;
  225.             }
  226.             break;
  227.             
  228.         case kEditMenuID:
  229.             switch(theItem)
  230.             {
  231.                 case kEditMenuCutItem:
  232.                     break;
  233.                     
  234.                 case kEditMenuCopyItem:
  235.                     break;
  236.                     
  237.                 case kEditMenuPasteItem:
  238.                     break;
  239.                     
  240.                 case kEditMenuPutColorItem:
  241.                     PutColorPickerIntoScrap(kRGBModel);
  242.                     break;
  243.             }
  244.             break;
  245.  
  246.         default:
  247.             break;
  248.     }
  249.     HiliteMenu(0);
  250.     DrawMenuBar();
  251. }
  252.  
  253. void DoExit(void)
  254. {
  255.  
  256. }
  257.  
  258. void main()
  259. {
  260.     DoTest();
  261.     DoInit();
  262.     DoLoop();
  263.     DoExit();
  264. }
  265.     
  266.